package com.tansun.ruledata.service.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tansun.rule.common.RuleModelTypeEnum;
import com.tansun.rule.config.AppConfiguration;
import com.tansun.rule.exception.RuleInvokeException;
import com.tansun.rule.utils.MClass;
import com.tansun.rule.utils.RuleParamsUtil;
import com.tansun.ruledata.data.entity.DeployDatacomponent;
import com.tansun.ruledata.data.entity.DeployVersionInfo;
import com.tansun.ruledata.data.service.BussinessSynchService;
import com.tansun.ruledata.data.service.RuleDeployResourceService;
import com.tansun.ruledata.data.service.RuleFlowService;
import com.tansun.ruledata.data.service.RuleResourceLoadService;
import com.tansun.ruledata.service.RuleFlowParamService;


@Service
public class RuleFlowParamServiceImpl implements RuleFlowParamService {
	@Autowired
	private  RuleFlowService  ruleFlowService;	
    @Autowired
	private  RuleDeployResourceService  RuleDeployResourceService;	
	@Autowired
	private  RuleResourceLoadService  ruleResourceLoadService;	
	@Autowired
	private BussinessSynchService bussinessSynchService;


	
	public Map<String, MClass> findDeployRuleFlowParam(String projectCode, String invokePoint) {
		Map<String, MClass> ruleFlowMetaMap = AppConfiguration.RuleFlowMetaMap.get(projectCode+"_"+invokePoint);
		Map<String,MClass> paramMap =  new HashMap<>();
		Map<String, MClass> metaMap = new HashMap<>();
		metaMap = AppConfiguration.RuleProjMetaMap.get(projectCode);
		if (metaMap == null || metaMap.isEmpty()){
			this.ruleResourceLoadService.loadProjectDataMapping(projectCode);
			metaMap = AppConfiguration.RuleProjMetaMap.get(projectCode);
		}
		if(1==1 || ruleFlowMetaMap==null || ruleFlowMetaMap.isEmpty()) {
			ruleFlowMetaMap = ruleFlowService.findDeployRuleFlowParam(projectCode, invokePoint, paramMap, metaMap);
			AppConfiguration.RuleFlowMetaMap.put(projectCode+"_"+invokePoint, ruleFlowMetaMap);
		}
		return ruleFlowMetaMap;
	}
	
	public  Map<String, Set<String>>  findRuleFlowParamByDrilFile(String projectCode, String invokePoint) throws Exception{
		Map<String,Set<String>> ruleParams = new HashMap<>();
		Map<String,Collection<DeployVersionInfo>> conMap=this.RuleDeployResourceService.findLatestRuleVersion(projectCode, invokePoint);
		if (conMap == null || conMap.isEmpty()){
			throw new RuleInvokeException("Drl文件加载失败！");
		}
		Collection<DeployVersionInfo>  list= conMap.get(invokePoint);
		 for (DeployVersionInfo ruleVersionVO :list){
			 if (!RuleModelTypeEnum.RuleFlow.code.equals(ruleVersionVO.getModelType())&& !RuleModelTypeEnum.ExecutionBlock.code.equals(ruleVersionVO.getModelType()) ){
					String content =  ruleVersionVO.getContont();
				    Pattern pattern = Pattern.compile(".*?ruleflow-group\\p{Blank}+\"((.*))\"");
					Matcher matcher = pattern.matcher(content);
					if (!matcher.find())
						continue;

					String rule = matcher.group(1);

					Set<String> params = new HashSet<>();
					params.addAll(RuleParamsUtil.matcherRuleFile(content));
					params.addAll(RuleParamsUtil.matcherOtherRuleFile(content));
					params.addAll(RuleParamsUtil.matcherRuleSetFile(content));
					params.addAll(RuleParamsUtil.matcherSetValue(content));
					params.addAll(RuleParamsUtil.matcherInsertValue(content));
					ruleParams.put(rule, params);
			 }
			
		}
		 return  ruleParams;
		
		
	}


	@Override
	public List<DeployDatacomponent> findDatacomponetParam(String projectCode, String invokePoint) {
		return this.ruleFlowService.findDataComponet(projectCode, invokePoint);
	}






}
